﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using PSTS.UI.ViewModels;
using PSTS.Models;

namespace PSTS.UI.Views.Pages
{
    /// <summary>
    /// Interaction logic for TestChapterSelectionPage.xaml
    /// </summary>
    public partial class TestChapterSelectionPage : Page
    {
        public TestChapterSelectionPage()
        {
            InitializeComponent();
            DataContext = new TestChapterSelectionPageViewModel();
        }

        public TestChapterSelectionPage(TestChapterSelectionPageViewModel viewModel)
        {
            InitializeComponent();
            DataContext = viewModel;
        }

        private void ChaptersList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var viewModel = DataContext as TestChapterSelectionPageViewModel;
            if (viewModel != null)
            {
                foreach (var item in e.AddedItems)
                {
                    var chapter = item as Chapter;
                    if (chapter != null)
                    {
                        ((TestChapterSelectionPageViewModel) DataContext).SelectedChapters.Add(chapter);
                    }
                    else
                    {
                        MessageBox.Show("Incorrect items in Chapters list on TestChapterSelectionPage");
                        break;
                    }
                }

                foreach (var item in e.RemovedItems)
                {
                    var chapter = item as Chapter;
                    if (chapter != null)
                    {
                        ((TestChapterSelectionPageViewModel) DataContext).SelectedChapters.Remove(chapter);
                    }
                    else
                    {
                        MessageBox.Show("Incorrect items in Chapters list on TestChapterSelectionPage");
                        break;
                    }
                }
            }
            else
            {
                MessageBox.Show("DataContext in TestChapterSelectionPage isn't set or set incorrect.");
            }
        }

        private void TopicsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var viewModel = DataContext as TestChapterSelectionPageViewModel;
            if (viewModel != null)
            {
                foreach (var item in e.AddedItems)
                {
                    var topic = item as Topic;
                    if (topic != null)
                    {
                        ((TestChapterSelectionPageViewModel)DataContext).SelectedTopics.Add(topic);
                    }
                    else
                    {
                        MessageBox.Show("Incorrect items in Topics list on TestChapterSelectionPage");
                        break;
                    }
                }

                foreach (var item in e.RemovedItems)
                {
                    var topic = item as Topic;
                    if (topic != null)
                    {
                        ((TestChapterSelectionPageViewModel)DataContext).SelectedTopics.Remove(topic);
                    }
                    else
                    {
                        MessageBox.Show("Incorrect items in Topics list on TestChapterSelectionPage");
                        break;
                    }
                }
            }
            else
            {
                MessageBox.Show("DataContext in TestChapterSelectionPage isn't set or set incorrect.");
            }
        
        }


    }

    public class BoolToVisibilityConverter : IValueConverter
    {
        public static readonly IValueConverter Instance = new BoolToVisibilityConverter();

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            bool param;
            var res = Visibility.Hidden;
            if (value is bool && parameter is string && bool.TryParse((string)parameter, out param))
            {
                res = (bool)value == param ? Visibility.Visible : Visibility.Hidden;
            }
            return res;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
